/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GUI.Customer;

import java.io.IOException;
import java.io.Serializable;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author SIX-14
 */
public class Server extends Thread implements Serializable{
    public Server(){

    }

    private ServerSocket server=null;
    private boolean finalized=false;
    private CustomerS comstumerS;

    public CustomerS getComstumer() {
        return comstumerS;
    }

    public void setComstumer(CustomerS comstumer) {
        this.comstumerS = comstumer;
    }

    public boolean isFinalized() {
        return finalized;
    }

    public void setFinalized(boolean finalized) {
        this.finalized = finalized;
    }

    public ServerSocket getServer() {
        return server;
    }

    public void setServer(ServerSocket server) {
        this.server = server;
    }


    public void startServer(){
        setFinalized(false);
            try {
               setServer(new ServerSocket(8490));
                
            } catch (IOException ex) {
            }
        comstumerS=new CustomerS();
            this.start();
    }

    public void stopServer(){
        setFinalized(true);
        try {
            if(comstumerS.getClientes()!=null){
                for(int i=0; i<comstumerS.getClientes().length; i++){
                    comstumerS.getClientes()[i].close();
                }
            }
            comstumerS.setClientes(null);
            getServer().close();
        } catch (IOException ex) {

        }
        setServer(null);
        this.stop();////-----------------------

    }

    public synchronized void run(){

        if(!isFinalized()){
            try {
                String[] ip=null;
                String ipnow=null;
                while(true){
                        Socket socketcliente=null;
                        socketcliente= getServer().accept();
                        ipnow=socketcliente.getInetAddress().toString();
                        if (ip==null){
                            comstumerS.setClientes(new ThreadSocket[1]);
                            comstumerS.getClientes()[0]=new ThreadSocket(socketcliente);
                            ip=new String[1];
                            ip[0]=socketcliente.getInetAddress().toString();

                        }else{
                            boolean token=false;
                            for(int i=0; i<ip.length;i++){
                                if(ip[i].equals(ipnow)){
                                    token=true;
                                    break;
                                }
                            }
                            if(token){
                                try{
                                    socketcliente.close();
                                }catch (IOException e2){

                                }
                            }else{
                                String[] ip2=new String[ip.length+1];
                                ThreadSocket[] socket2= new ThreadSocket[comstumerS.getClientes().length+1];
                                for(int i=0; i<comstumerS.getClientes().length; i++){
                                    socket2[i]=comstumerS.getClientes()[i];
                                    ip2[i]=ip[i];
                                }
                                socket2[comstumerS.getClientes().length]=new ThreadSocket(socketcliente);
                                comstumerS.setClientes(socket2);
                                ip2[ip.length]= socketcliente.getInetAddress().toString();
                                ip=ip2;
                            }
                        }
                    }


                } catch (IOException ex) {
                    
                }

            }
        }

    }
